/**
 * @asignatura: Procesadores de Lenguajes.
 * @autor:      Anthony Vittorio Russo Cabrera
 * @curso:      2012/2013
 * @practica:   06 - ANTLR. Ambigüedades Sintácticas
 * @descripcion:
 */
package pl.p07;

import java.io.FileNotFoundException;
import java.io.IOException;

import org.antlr.runtime.ANTLRFileStream;
import org.antlr.runtime.ANTLRInputStream;
import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.Token;

import pl.p07.antlr3.YACCLexer;
import pl.p07.antlr3.YACCParser;
import pl.p07.modification.Blocks;
import pl.p07.modification.Keywords;
import pl.p07.modification.LexicalException;
import pl.p07.modification.SourceFile;

public class Procesador {
  
  public static void lexerYACC(ANTLRStringStream input) {
    YACCLexer lexer = new YACCLexer(input);
    Token token;
    for (token = lexer.nextToken(); token.getType() != Token.EOF; token = lexer.nextToken()) {
      System.out.println(token);
    }// for
  }// lexerYACC

  public static void parserYACC(ANTLRStringStream input) {
    try {
      YACCLexer lexer = new YACCLexer(input);
      CommonTokenStream tokens = new CommonTokenStream(lexer);
      YACCParser parser = new YACCParser(tokens);
      parser.doumentYACC();
    } catch (RecognitionException e) {
      System.err.println("Parser Error");
      e.printStackTrace();
    }// try-catch
  }// runYACC
 
 public static void main(String args[]) {
    // Determinar parámetros
    ANTLRStringStream input = null;
    try {
      switch (args.length) {
        case 1: // InputStream | FileStream
          try {
            Integer.parseInt(args[0]);
            input = new ANTLRInputStream(System.in);
            parserYACC(input);
          } catch (NumberFormatException e) {
            // args[0] no es un número
            //input = new ANTLRFileStream(args[0]);
            //lexerYACC(input);
            input = new ANTLRFileStream(args[0]);
            parserYACC(input);
            //----------------------------------------------------------------|
            // Modificación: similar a la práctica 02
            //
            System.out.println("file: '" + args[0] + "'");
            Blocks blocks = new Blocks();
            Keywords keywords = new Keywords("./res/io/modification/keywords.txt");
            SourceFile source = new SourceFile(args[0]);
            LexicalException.file = source;

            String line = null;
            while ((line = source.readLine()) != null) {
              try {
                keywords.parse(line);
                blocks.parse(line);
              } catch (LexicalException e1) {
                System.err.println(e1.getErrorDescription());
              }// try-catch
            }// while
            source.close();

            System.out.print(keywords.report());
            System.out.print(blocks.report());

            try {
              blocks.check();
            } catch (LexicalException e1) {
              System.err.println(e1.getErrorDescription());
            }// try-catch
            //----------------------------------------------------------------|
          }// try-catch
          break;
        default:
          System.err.println("java Procesador (option | file)");
          System.exit(0);
      }// switch

    // Execpciones
    } catch (FileNotFoundException e) {
      System.err.println("File Not Found: \"" + args[0] + "\"");
      e.printStackTrace();
    } catch (IOException e) {
      System.err.println("Input/Output Error");
      e.printStackTrace();
    }// try-catch
  } // main

}// Procesador